﻿<%@ Page Language="C#" AutoEventWireup="true" enableEventValidation="false" CodeBehind="qst_manage_usersel.aspx.cs" Inherits="Ccit.OA.Question.qst_manage_usersel" %>
<%@ Register Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="Microsoft.Web.UI.WebControls" TagPrefix="iewc" %>
    
<%@ Register TagPrefix="ccit" Namespace="Ccit.ServerControls" Assembly="Ccit.ServerControls.ActionForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
   <script language="javascript" type="text/javascript" src="../script/jquery.js"></script>
    <script language="javascript" type="text/javascript" src="../script/StringClass_Ext.js"></script>
    <script language="javascript" type="text/javascript" src="../script/jsonlib.js"></script>
    <script language="javascript" src="../script/jwindow.js" type="text/javascript"></script>
    <script language="javascript" src="../script/jcontrol.js" type="text/javascript"></script>
    <script language="javascript" src="../script/jbase64.js" type="text/javascript"></script>
</head>
<body>
    <ccit:ActionForm id="form1" runat="server">
    <!--------问卷ID------------------------->
    <input type="hidden" id="_hidType" runat="server" />
    <input type="hidden" id="_hidsRecId" runat="server" />
    <input type="hidden" id="_hidUserID" runat="server" />
    <!--绑定树的标志-->
    <input type="hidden" id="_hidFlag" runat="server" />
    <!--组织机构的根节点id-->
    <input type="hidden" id="_hidRootId" runat ="server" />
    <!--根节点选中Userid-->
    <input type="hidden" id="_hidCheckUserId" runat ="server" />
    <!--选中节点id-->
    <input type="hidden" id="_hidSelNodeId" runat ="server" />
    <!--判断选中节点是否有子节点-->
    <input type="hidden" id="_hidHaveChild" runat ="server" />
    <!--选中节点类型-->
    <input type="hidden" id="_hidNodeType" runat ="server" />
    <!--找到的节点id-->
    <input type="hidden" id="_hidFindId" runat ="server" />
    
    <input type="hidden" id="_hidLevelStruts" runat ="server" />
    
    <input type="hidden" id="_hidExpandSign" runat="server" />
    
    <iewc:TreeView ID="tvUser" runat="server" onclick="selectedNode()">
    </iewc:TreeView>
     <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <input type="button" id="btnTrigger" onclick="doTrigger()" style="display: none" value="触发事件" />
    
    <script type="text/javascript" language="javascript">
    
      var node = null;
      
      function selectedNode()
      {
        var nodeIndex = document.getElementById("tvUser").clickedNodeIndex
        var node = document.getElementById("tvUser").getTreeNode(nodeIndex);
        if(node == null) return ;
        var vNodeId = node.getAttribute("ID");
        var vNodeArray = vNodeId.split("_");
        var vPrimaryId = vNodeArray[0];
        var vExpandSign = vNodeArray[1];
        var vType = vNodeArray[2]
        
        var hasChild =( node.getChildren().length > 0) ? "Y" : "N"; 
        document.getElementById("_hidSelNodeId").value = vPrimaryId;
        document.getElementById("_hidHaveChild").value = hasChild;
        document.getElementById("_hidNodeType").value = vType;
        
      }
      
      function tvUser.onexpand()
      {
          var nodeIndex = event.treeNodeIndex;
          if(typeof(nodeIndex) == "undefined")
          {
             node = null;
             return false;
          }
          tvUser.selectedNodeIndex = nodeIndex;
          node = tvUser.getTreeNode(nodeIndex);
          
          var vNodeId = node.getAttribute("ID");
          var vNodeArray = vNodeId.split("_");
          var vPrimaryId = vNodeArray[0];
          var vExpandSign = vNodeArray[1];
          var vType = vNodeArray[2]
          
          var hasChild =( node.getChildren().length > 0) ? "Y" : "N"; 
          document.getElementById("_hidSelNodeId").value = vPrimaryId;
          document.getElementById("_hidHaveChild").value = hasChild;
          document.getElementById("_hidNodeType").value = vType;
          
          
          if(vExpandSign == "0" && vPrimaryId != "0")
          {
             var oSubNodes = node.getChildren();
             var oSubNode;
             for(var i = oSubNodes.length - 1; i >= 0 ; i --)
             {
                oSubNode = oSubNodes[i];
                oSubNode.remove();
             }
             
             var pageFlag = $get("_hidFlag").value;
             var vsRecId = document.getElementById("_hidsRecId").value;
             var vtype = document.getElementById("_hidType").value;
             if(pageFlag == "1") //组织机构
             {
                var levelStruts = document.getElementById("_hidLevelStruts").value;
                
                PageMethods.GetOrganSubNodes(vPrimaryId,nodeIndex,levelStruts,vsRecId,vtype,ClientCallBack);
             } 
          }
      }
      function ClientCallBack(vResult)
      {
          var vResultArray = vResult.split("_");
          var vType = vResultArray[0];
          
          switch(vType)
          {
            case "dept" :
                 var vNodeIndex = vResultArray[1];
                 var vChildXML = vResultArray[2];
                 var bHaveUser = vResultArray[3];
                 var vlstUser = vResultArray[4];
                 
                 var levelStruct = vResultArray[5];
                 var parentNodeId = vResultArray[6];
                 
                 var oXMLDoc = new ActiveXObject("Microsoft.XMLDOM");  
                 oXMLDoc.loadXML(vChildXML);
                 createOrganSubTreeNodes(vNodeIndex,vChildXML,vlstUser);
                 
                 var expandSign = document.getElementById("_hidExpandSign").value;
                 if(expandSign == "1") 
                 {    //是逐级展开
                    var parentIdArr = levelStruct.split(",");
                    var iLen = parentIdArr.length;
                    var bGet = false;
                    var findNodeIndex;
                    
                    for(var i = 0; i < iLen; i ++) 
                    {                      
                        var colId = parentIdArr[i];     
                        var childArray = colId.split("~");
                        var setCtrl;
                        setCtrl = findTreeNode(childArray[0],childArray[1]);
                        findNodeIndex = setCtrl.getNodeIndex();
                         
                        if(i<iLen-1)
                        {
                            if(!bGet) 
                            {
                                if(childArray[0] == parentNodeId)
                                {
                                    if(i == (iLen - 1)) 
                                    {   //如果到最底层,则逐级展开标志0
                                        $get("_hidExpandSign").value = "0";                                
                                    }
                                    bGet = true;
                                    continue;
                                }
                            }
                            else //true,展开下级
                            {
                                var ctrlNode;
                                
                                ctrlNode = findTreeNode(childArray[0],childArray[1]);
                                var nodeAttrId = ctrlNode.getAttribute("ID");    
                                var vNodeIdArray = nodeAttrId.split("_");
	                            if(vNodeIdArray[1] == "1")
	                            {
	                                if(i == (iLen - 1)) 
	                                {
                                        ctrlNode.setAttribute("EXPANDED",true);                               
                                    }
                                    else
	                                    continue;
	                            }
	                            else 
	                            {
	                                ctrlNode.setAttribute("EXPANDED",true);
	                                break;
	                            }
	                             var selId = parentIdArr[iLen-1];     
                                 var setNodeArray = selId.split("~");
                            
                                 var selCtrl = findTreeNode(setNodeArray[0],setNodeArray[1]);
                                
	                             findNodeIndex = selCtrl.getNodeIndex();
                            }
                        }
                      }
                      document.getElementById("tvUser").selectedNodeIndex = findNodeIndex;
                  }   
                         break;  
                  objResult = null;     
                       
          }
          oXMLDoc = null;
      }
      
      /* 创建组织机构下的子节点 */
      function createOrganSubTreeNodes(nodeIndex,strXML,vlstUser)
      {      
             strXML = strXML.replace(/&lt;/g,'<');
             strXML = strXML.replace(/&gt;/g,'>');
             var node = $get("tvUser").getTreeNode(nodeIndex);
             var oXMLDoc = new ActiveXObject("Microsoft.XMLDOM");
             oXMLDoc.loadXML(strXML);
             var oSubNode;
             var pChecked = node.getAttribute("checked");
             var vUserInfo = document.getElementById("_hidUserID").value;
             var vUId = vUserInfo.split(",");
             var vIcount=0;
             $(oXMLDoc).find('DEPT').each(function(){
              var userId = $(this).find("USERID").text();
              var STATE = $(this).find("STATE").text();
              if(userId!="")
              {                    
                  var suserId = userId.split(",");
                  for(var i=0;i<suserId.length;i++)
                  {
                      for(var j=0;j<vUId.length;j++)
                       {
                            if(suserId[i]==vUId[j])
                            { 
                                STATE="1"
                                vIcount=1;
                                break;
                            }
                            else
                            { 
                                vIcount=0;
                            }
                       }
                   }
                 if(vIcount==1||STATE!="0")
                 {
                     var nodeText = $(this).find("DEPTNAME").text();
                     var nodeId = $(this).find("DEPTID").text();
                     var childValue = $(this).attr("HASCHILD"); 
                     oSubNode = $get("tvUser").createTreeNode();
                     oSubNode.setAttribute("ID",nodeId + "_0_dept");
                     oSubNode.setAttribute("Text",nodeText);
                     oSubNode.setAttribute("checkBox","true");
                     if(pChecked == true)
                     {
                        oSubNode.setAttribute("checked","true");
                     }
                     if(childValue == "1")
                     {  
                        var oEmptyNode = $get("tvUser").createTreeNode();
                        oSubNode.add(oEmptyNode);
                     } 
                     node.add(oSubNode);
                 }    
               }  
             });
             
             eval(" var vlstUserResult = " + vlstUser); 
             var vQId = document.getElementById("_hidsRecId").value;
             for(var i = 0 ; i < vlstUserResult.length; i ++)
             { 
                var vChildObj = vlstUserResult[i];
                var CheckUserId="";
                for(var j=0;j<vUId.length;j++)
                {
                    if(vChildObj.userId==vUId[j])
                    { 
                        oSubNode = $get("tvUser").createTreeNode();
                        oSubNode.setAttribute("checkBox","true");
                        oSubNode.setAttribute("ID",vChildObj.userId + "_0_user");
                        oSubNode.setAttribute("Text",vChildObj.userName); 
                        if(vChildObj.effect=="")
                        {
                            vChildObj.userId="";
                        }
                        else
                        {
                            
                            if(CheckUserId=="")
                            {
                                CheckUserId = vChildObj.userId;
                                document.getElementById("_hidCheckUserId").value = CheckUserId;
                                oSubNode.setAttribute("checked","true");
                            }
                        }
                        var organPageParam="QId="+vQId+"&userId="+vChildObj.userId+"";    //用户
                        var organEncodeParam=base64encode(organPageParam);
                        oSubNode.setAttribute("NavigateUrl","qst_manage_logView.aspx?"+organEncodeParam); 
                        oSubNode.setAttribute("Target","iFrmEdit"); 
                       
                        if(pChecked == true)
                        {
                            oSubNode.setAttribute("checked","true");
                        }
                        node.add(oSubNode); 
                    }
                } 
             }
             var vNodeId = node.getAttribute("ID");
             var vArray = vNodeId.split("_");
             
             node.setAttribute("ID",vArray[0] + "_1_dept");
      }
       
        /* oncheck事件 */
        function tree_oncheck(tree)
        {
             var node=tree.getTreeNode(tree.clickedNodeIndex);
             var Pchecked=tree.getTreeNode(tree.clickedNodeIndex).getAttribute("checked");
             setcheck(node,Pchecked); 
        }
        
        /* 设置子节点选中 */
        function setcheck(node,Pc)
        {
         var i;
         var ChildNode=new Array();
         ChildNode=node.getChildren();
         
         if(parseInt(ChildNode.length)==0)
          return;
         else
         { 
           for(i=0;i<ChildNode.length;i++)
           {
             var cNode;
             cNode=ChildNode[i];
             if(parseInt(cNode.getChildren().length)!=0)
             setcheck(cNode,Pc);
             cNode.setAttribute("checked",Pc);
           }
          }
        } 
        
         var cNodeIds="";
        /* 获取所有选中节点 */
         function FindCheckedFromNode(node)
         {
            cNodeIds=""
            var nodes = new Array();
              
            if(node==null){
               var node=tvUser.getTreeNode("0");   
               nodes = node.getChildren();
            }
            else
            {
               nodes = node.getChildren();
            }
            for (var i = 0; i < nodes.length; i++) {
              var cNode=nodes[i];
              if (cNode.getAttribute("checked")){
                 var checkNodeId=cNode.getAttribute("ID");
                   if(checkNodeId != "")
                        cNodeIds += (cNodeIds == "" ) ? checkNodeId : "," + checkNodeId;
            }
            if (parseInt(cNode.getChildren().length) != 0 )
                  FindCheckedFromNode(cNode);
            } 
        }
        
        function GetcheckUserIds()
        {   
            FindCheckedFromNode();
            return cNodeIds;
        }
        
        
        /* 循环展开节点 */
        function doTrigger() {
            var oFindId = document.getElementById("_hidFindId").value;
            var nodeType = document.getElementById("_hidNodeType").value;
            var ctrlNode = findTreeNode(oFindId,nodeType);
            if(null == ctrlNode) {    
		         expandNode(oFindId,nodeType);
		         return;
		    }
		    
		    
		    var findNodeIndex = ctrlNode.getNodeIndex();
		   // var parentNodeArr = [];   //展开其父及节点(二)
		   // parentNodeArr.push(ctrlNode);
		   // getParentNode(ctrlNode,parentNodeArr); 
		   //   expandParentNode(parentNodeArr);		    
		    expandParent(ctrlNode); //展开父节点(一)
		   
		    document.getElementById("tvUser").selectedNodeIndex = findNodeIndex;
        }
        
        /* 获取上级父节点,将其展开 */
        
        // 从最里面开始展开(一)
		function expandParent(dtNode)
		{
		    var parNode = dtNode.getParent();
		   parNode.setAttribute("EXPANDED",true);
		   if(parNode.getNodeIndex() != "0")
		      expandParent(parNode);   
		}

        // 从最外面开始展开	(二)
//		function getParentNode(ctrlNode,parentNodeArr)
//		{ 
//		   var parNode = ctrlNode.getParent();
//		   parentNodeArr.push(parNode);
//		   if(parNode.getNodeIndex() != "0") {		      
//		      getParentNode(parNode,parentNodeArr);
//		   }
//		}
		
//		function expandParentNode(parentNodeArr) {
//		    if (null != parentNodeArr && parentNodeArr != "undefined") {
//		        var itemCount = parentNodeArr.length;
//		        var node;
//		        for(var i = itemCount - 1; i >= 0; i --) {
//		            node = parentNodeArr[i];
//		            node.setAttribute("EXPANDED",true);
//		        }
//		    }
//		}
        
        function expandNode(dtNodeId,nodeType) {
		    $get("_hidExpandSign").value = "1";//连续展开标志
		    PageMethods.GetNodeLevelStruct(dtNodeId,nodeType,ClientCallBack);  //获取新父节点上级节点
		}
      
        /* 返回查到的节点对象 */
        function findTreeNode(oFindId,nodeType) {
            var ctrlTree = document.getElementById("tvUser");		    
		    
		    var ctrlRoot = ctrlTree.getTreeNode("0"); //获取根节点
		    var ctrlChildNodes = ctrlRoot.getChildren();  //所有子节点
	    
		    var ctrlFindNode = recursionFind(ctrlChildNodes,oFindId,nodeType);
            return ctrlFindNode;
        }
        
        /* 循环查找返回查到的节点对象 */
        function recursionFind(ctrlChildNodes,oFindId,nodeType) {
            var iLen = ctrlChildNodes.length;
            var ctrlTreeNode;
		    var vNodeId;
		    var vNodeIdArray;
		    var vAstTypeId;
		    var ctrlSubChildNodes;
		    var bFind = false;
		    
            for(var i = 0; i < iLen; i ++) {
		        ctrlTreeNode = ctrlChildNodes[i];
		        vNodeId = ctrlTreeNode.getAttribute("ID");    
		        
		        var oFindTreeId0 = oFindId +"_0_" + nodeType;
		        var oFindTreeId1 = oFindId +"_1_" + nodeType;
		        if(vNodeId == oFindTreeId0 || vNodeId == oFindTreeId1){		           
		            bFind = true;
		            break;       
		        }
		        else {
		            ctrlSubChildNodes = ctrlTreeNode.getChildren();
		            ctrlTreeNode = recursionFind(ctrlSubChildNodes,oFindId,nodeType);
		            if(null != ctrlTreeNode) {
		                bFind = true;
		                break;
		            }
		        }
		    }
            
            if(bFind)
                return ctrlTreeNode;
            else
                return null;
        } 
    </script>
    <script type="text/javascript">
    <!--
        $(document).ready(function(){ 
          var vIndex = document.getElementById("_hidCheckUserId").value;
          var nodes = new Array();
          var node=tvUser.getTreeNode("0");   
          nodes = node.getChildren();
          for (var i = 0; i < nodes.length; i++) {
               var cNode=nodes[i]; 
               var checkNodeId=cNode.getAttribute("ID");
                var vNodeArray = checkNodeId.split("_");
                var vPrimaryId = vNodeArray[0];
               if(vIndex==vPrimaryId)
               {
                   var nodeIndex = cNode.getNodeIndex();
                   tvUser.selectedNodeIndex = nodeIndex;
                   break;
               }
            } 
        
        
            var vQId = document.getElementById("_hidsRecId").value; 
            var userId = document.getElementById("_hidCheckUserId").value; 
            var organPageParam="QId="+vQId+"&userId="+userId+"";    //问卷ID 和用户ID
            var organEncodeParam=base64encode(organPageParam);
            var URL = "qst_manage_logView.aspx?"+organEncodeParam;
            parent.frames[1].document.location.href= URL;
            
        }); 
    //-->
    </script>
    </ccit:ActionForm>
</body>
</html>
